<?php
class Account_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();

    }
    function getMemberByAccount($account)
    {   
        $query = $this->db->query("select * from member nolock where account='".$account."' limit 1");
        return $query->row();
    }

    public function incrCredit($uid,$amount = 1)
    {
        $this->db->query("update member set credit = credit +  ".$amount." where uid=".$uid);
        return $this->db->affected_rows()>0;
    }

    public function decrCredit($uid,$amount = 1)
    {
        $credit = $this->queryUserCredit($uid);

        if($credit->credit>=$amount)
        {
            $this->db->query("update member set credit = credit -  ".$amount." where uid=".$uid);
            return TRUE;
        }
        return FALSE;
    }

    public function queryUserCredit($uid)
    {
        $query = $this->db->query("select credit from member nolock where uid=".$uid);
        return $query->row();
    }

    function getMemberById($uid)
    {   

        $query = $this->db->query("select * from member  where uid=".$uid." limit 1");

        return $query->row();
    }
    function updateLoginTime($uid,$lastloginip)
    {
        $this->db->query("update member set logincount = logincount+1,lastlogintime=".time().",lastloginip='".$lastloginip."' where uid=".$uid);
        $this->db->query("insert into loginlog (uid,loginip,logintime) values (".$uid.",'".$lastloginip."',".time().")");
    }
    function checkAccount($account)
    {
        $query = $this->db->query("select 1 from member nolock where account='".$account."'");
        return $query->num_rows();
    }
    
    function create($accountinfo)
    {
        $accountinfo['status'] = 0;

        $query = $this->db->insert('member', $accountinfo); 
        return $this->db->insert_id();
    }

    function modifypasswd($uid,$password,$slat)
    {
        $query = $this->db->query("update member set passwd='".$password."',slat='".$slat."' where uid=".$uid." limit 1");
    }


    function modifyaccount($account)
    {
        $sql="update member set username='".$account['realname']."',realname='".$account['realname']."',gender={$account['gender']},birthday='".$account['birthday']."',location='".$account['location']."' where uid={$account['uid']} limit 1";
        $this->db->query($sql);
    }

    function getUserECard($uid)
    {
        $query = $this->db->query("select uid,sn,code,createtime,expire from memberecard nolock  where uid=".$uid." limit 1");
        return $query->row();
    }

    function bindECard($cardinfo)
    {
        $query = $this->getUserECard($cardinfo['uid']);
        if($query)
            return $query; 
        $sql="insert into memberecard(uid,sn,code,createtime,expire) values ({$cardinfo['uid']},'".$cardinfo['sn']."','".$cardinfo['code']."',".time().",90)";
        $this->db->query($sql);
        return $cardinfo;
    }
    function unBindECard($uid,$sn)
    {
        $this->db->query("delete from memberecard where uid=".$uid." and sn='".$sn."'");
        return $this->db->affected_rows() > 0;
    }

    /**
    更改头像
    */
    function changeface($uid,$face)
    {
        $this->db->query("update member set userface='".$face." where uid=".$uid." limit 1");
        return $this->db->affected_rows() > 0;
    }


    function getAll($page=1,$pagesize=100,$condition=array())
    {
        $count = 0;
        $limit = ($page-1) * $pagesize;
        $where = ' where 1 = 1 ';
        if($condition)
        {
            foreach($condition as $key=>$val)
            {
                $where .=" and `".$key."` = '".$val."' ";
            }
        }
        $query = $this->db->query("select count(0) as count from member nolock ".$where);
        $count = $query->row()->count;
        $query = $this->db->query("select * from member nolock ".$where." limit ".$limit.",".$pagesize);
        $data = array();
        foreach ($query->result_array() as $row)
        {
            $data[] = $row;
        }
        $query->free_result();
        return array('count'=>$count,'data'=>$data);
    }

    function lock($uid)
    {
        $this->db->query("update member set status = 1 where uid=".$uid." limit 1");
        return $this->db->affected_rows() > 0;
    }

    function unlock($uid)
    {
        $this->db->query("update member set status = 0 where uid=".$uid." limit 1");
        return $this->db->affected_rows() > 0;
    }

}
?>